
package com.putao.album.main.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.putao.album.R;
import com.putao.album.child.bean.Child;
import com.putao.album.main.bean.RequestSideMoreInfo;
import com.putao.album.util.CommonUtils;
import com.putao.album.util.DisplayHelper;

import java.util.ArrayList;

/**
 * Created by yanglun on 15/4/30.
 */
public class BabyHeaderIconCollectionView extends LinearLayout {

    private OnClickListener mSelectChildClickListener;
    private OnClickListener mAddChildClickListener;
    private ArrayList<RequestSideMoreInfo.SideMoreInfo.BabyInfo> mBabyInfoArray;
    private BabyHeaderIconView mAddView;

    public BabyHeaderIconCollectionView(Context context) {
        super(context);
        init();
    }

    public BabyHeaderIconCollectionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BabyHeaderIconCollectionView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        this.setOrientation(LinearLayout.HORIZONTAL);
        mBabyInfoArray = new ArrayList<RequestSideMoreInfo.SideMoreInfo.BabyInfo>();
        mAddView = new BabyHeaderIconView(getContext());
        mAddView.header_icon_name_tv.setText("添加...");
        mAddView.header_icon_gender_iv.setVisibility(View.GONE);
        mAddView.setTypeAddBaby();
        mAddView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public void removeAllBabyHeaderInfo() {
        mBabyInfoArray.clear();
    }

    public void addBabyHeaderInfo(RequestSideMoreInfo.SideMoreInfo.BabyInfo info) {
        mBabyInfoArray.add(info);
    }

    public void show() {

        this.removeAllViews();
        for (int i = 0; i < mBabyInfoArray.size(); i++) {
            RequestSideMoreInfo.SideMoreInfo.BabyInfo info = mBabyInfoArray.get(i);
            if (info.id.equals(CommonUtils.getBabyId())) {
                addBabyHeaderView(mBabyInfoArray.get(i), true);
                break;
            }

        }
        for (int i = 0; i < mBabyInfoArray.size(); i++) {
            RequestSideMoreInfo.SideMoreInfo.BabyInfo info = mBabyInfoArray.get(i);
            if (!info.id.equals(CommonUtils.getBabyId())) {
                addBabyHeaderView(mBabyInfoArray.get(i), false);
            }
        }
        this.addView(mAddView);
    }


    private void addBabyHeaderView(RequestSideMoreInfo.SideMoreInfo.BabyInfo info, boolean isCurrentUser) {
        BabyHeaderIconView view = new BabyHeaderIconView(getContext());

        if (isCurrentUser) {
            view.header_icon_iv.setLayoutParams(new LinearLayout.LayoutParams(DisplayHelper.dipTopx(80), DisplayHelper.dipTopx(80)));
        }
        DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.home_page_header_icon)
                .showImageOnFail(R.drawable.home_page_header_icon).cacheInMemory(true).cacheOnDisc(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).build();

        ImageLoader.getInstance().displayImage(info.avatar, view.header_icon_iv, options);

        view.header_icon_name_tv.setText(info.name);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        if (info.sex.equals(Child.ChildSex.boy)) {
            view.header_icon_gender_iv.setImageResource(R.drawable.all_icon_boy_border);
        } else if (info.sex.equals(Child.ChildSex.girl)) {
            view.header_icon_gender_iv.setImageResource(R.drawable.all_icon_girl_border);
        } else if (info.sex.equals(Child.ChildSex.noBorn)) {
            view.header_icon_gender_iv.setImageResource(R.drawable.all_icon_unknow_border);
        }
        if (mSelectChildClickListener != null) {
            view.setTag(info);
            view.setOnClickListener(mSelectChildClickListener);
        }

        LayoutParams a = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        a.gravity = Gravity.BOTTOM;

//        view.setGravity(Gravity.BOTTOM);
        addView(view, a);
    }

    public void setAddChildClickListener(OnClickListener listener) {
        mAddChildClickListener = listener;
        if (mAddView != null) {
            mAddView.setOnClickListener(mAddChildClickListener);
        }

    }

    public void setSelectChildClickListener(OnClickListener listener) {
        mSelectChildClickListener = listener;
    }
}
